package com.guanyong.services;

import com.guanyong.dao.KeyBaseDaoMapper;
import com.guanyong.dao.KeyBaseJavaDaoMapper;
import com.guanyong.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by mxk on 2017/7/3.
 */
@Service
public class KeybaseService {

    @Autowired
    KeyBaseDaoMapper keyBaseDaoMapper;
   @Autowired
   KeyBaseJavaDaoMapper keyBaseJavaDaoMapper;

//    private  static String dateEnd="2017-09-29";
    private  static String dateEnd="2017-09-28";

    public List<KeyPiaofangModel> getPiaofangList() {
        return keyBaseDaoMapper.getLodPiaofang();
    }

    public KeyPiaofangModel getNowPiaofang() {
        return keyBaseDaoMapper.getNowPiaofang(getNowDate());
    }



    public List<KeyBaseModel> selectByAll(){
        return  keyBaseDaoMapper.selectByAll();
    }

    public List<KeyWeiboZhishuCountModel> getKeyWeiboZhishuAll() {

        SimpleDateFormat dft = new SimpleDateFormat("MM.dd");
        Date beginDate = new Date();
        //前一天
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 31);
        String dateStr=dft.format(date.getTime());
        String tempDate = dft.format(beginDate);
        List<String> dateList = new ArrayList<String>();
        dateList.add(tempDate);
        try {
            while (!tempDate.equals(dateStr)){
                date.setTime(dft.parse(tempDate));
                date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
                tempDate = dft.format(date.getTime());
                dateList.add(tempDate);
            }
            dateList = dateList.subList(1,dateList.size()-1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Collections.reverse(dateList);

        List<KeyWeiboZhishuModel> list = keyBaseDaoMapper.getWeiboZhishuAll();
        List<KeyWeiboZhishuCountModel> listCount = new ArrayList<KeyWeiboZhishuCountModel>();
        Map<Integer,KeyWeiboZhishuCountModel> keyWeiboZhishuMap = new HashMap<Integer,KeyWeiboZhishuCountModel>();
        for (KeyWeiboZhishuModel keyWeiboZhishuModel:list){
            if (keyWeiboZhishuMap.get(keyWeiboZhishuModel.getBaseId())==null){
                KeyWeiboZhishuCountModel keyWeiboZhishuCountModel = new KeyWeiboZhishuCountModel();
                keyWeiboZhishuCountModel.setBaseId(keyWeiboZhishuModel.getBaseId());
                keyWeiboZhishuCountModel.setName(keyWeiboZhishuModel.getName());
                keyWeiboZhishuMap.put(keyWeiboZhishuModel.getBaseId(),keyWeiboZhishuCountModel);
            }
            List<String> temp = keyWeiboZhishuMap.get(keyWeiboZhishuModel.getBaseId()).getTrentList();
            if (temp.size()<31){
                temp.add(keyWeiboZhishuModel.getTrent());
            }
        }
        for (Integer key : keyWeiboZhishuMap.keySet()) {
            keyWeiboZhishuMap.get(key).setDateList(dateList);
            listCount.add(keyWeiboZhishuMap.get(key));
        }

        return listCount;
    }


    public List<KeyPingfenModel> getKeyPingfenByDay() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr =formatter.format(date);
//        dateStr+=" 00:00:00";
        List<KeyPingfenModel> list=keyBaseDaoMapper.getKeyPingfenByDay(dateStr);
        Collections.sort(list);
        return list;
    }


    public List<KeyBozhuModel> getKeyBozhuTop5() {
        return keyBaseDaoMapper.getBozhuTop5(getNowDate());
    }


    public List<KeyHuatiModel> getKeyHuatiTop7() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
        Date endDate =null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
            dft.format(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

//        List<KeyHuatiModel> oldList =keyBaseDaoMapper.getHuatiList(dft.format(endDate));
//        List<KeyHuatiModel> noList =keyBaseDaoMapper.getHuatiList(dft.format(beginDate));
        List<KeyHuatiModel> oldList =keyBaseDaoMapper.getHuatiList("2017-09-28");
        List<KeyHuatiModel> noList =keyBaseDaoMapper.getHuatiList("2017-09-29");
        for (KeyHuatiModel keyHuatiModel:oldList){
            keyHuatiModel.setReadNum(strToNum(keyHuatiModel.getReadNumStr()));
            keyHuatiModel.setDiscussNum(strToNum(keyHuatiModel.getDiscussNumStr()));

        }
        for (KeyHuatiModel keyHuatiModel:noList){
             keyHuatiModel.setReadNum(strToNum(keyHuatiModel.getReadNumStr()));
             keyHuatiModel.setDiscussNum(strToNum(keyHuatiModel.getDiscussNumStr()));
            keyHuatiModel.setTab(1);
        }
        Collections.sort(oldList);
        Collections.sort(noList);
        for (int i =0;i<7;i++){
            for (int j =0;j<7;j++){
                if (noList.get(i).getUrl().equals(oldList.get(j).getUrl())){
                    if (i==j){
                        noList.get(i).setTab(0);
                    }else if (i>j){
                        noList.get(i).setTab(-1);
                    }else {
                        noList.get(i).setTab(1);
                    }
                }
            }
        }
        return noList.subList(0,7);
    }



    public List<KeyPlayCountModel> getPlayCount() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        List<KeyPlayCountModel> listNow= new ArrayList<KeyPlayCountModel>();
        List<KeyPlayCountModel> listOld= new ArrayList<KeyPlayCountModel>();
        try {
            beginDate =dft.parse( dft.format(beginDate));
            String dateStr = dft.format(beginDate);

            //前一天
            Calendar date = Calendar.getInstance();
            date.setTime(dft.parse(dateStr));
            date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
            dateStr=dft.format(date.getTime());
            String nowDate = dateStr;
            //前2天
            date.setTime(dft.parse(dateStr));
            date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
            String dateOldStr=dft.format(date.getTime());


//            nowDate="2017-09-30";
//            dateOldStr="2017-09-29";
            List<KeyPlay2Model> nowList=keyBaseDaoMapper.getPlayCountAll(nowDate);
            List<KeyPlay2Model> oldList=keyBaseDaoMapper.getPlayCountAll(dateOldStr);

            Map<String,KeyPlayCountModel> nowMap=new HashMap<String, KeyPlayCountModel>();
            Map<String,KeyPlayCountModel> oldMap=new HashMap<String, KeyPlayCountModel>();
            for (KeyPlay2Model keyPlay2Model:nowList){
                KeyPlayCountModel keyPlayCountModel;
                if (nowMap.get(keyPlay2Model.getBaseId().toString())==null){
                    keyPlayCountModel = new KeyPlayCountModel();
                    keyPlayCountModel.setBaseId(keyPlay2Model.getBaseId());
                    keyPlayCountModel.setName(keyPlay2Model.getName());
                    nowMap.put(keyPlay2Model.getBaseId().toString(),keyPlayCountModel);
                }
                keyPlayCountModel =nowMap.get(keyPlay2Model.getBaseId().toString());

                if (keyPlay2Model.getType()==2){
                    keyPlayCountModel.setQqCount(keyPlay2Model.getCount());
                }else if (keyPlay2Model.getType()==3){
                    keyPlayCountModel.setYoukuCount(keyPlay2Model.getCount());
                }else if (keyPlay2Model.getType()==4){
                    keyPlayCountModel.setIqiyiCount(keyPlay2Model.getCount());
                }

            }

            for (KeyPlay2Model keyPlay2Model:oldList){
                KeyPlayCountModel keyPlayCountModel;
                if (oldMap.get(keyPlay2Model.getBaseId().toString())==null){
                    keyPlayCountModel = new KeyPlayCountModel();
                    keyPlayCountModel.setBaseId(keyPlay2Model.getBaseId());
                    keyPlayCountModel.setName(keyPlay2Model.getName());
                    oldMap.put(keyPlay2Model.getBaseId().toString(),keyPlayCountModel);
                }
                keyPlayCountModel =oldMap.get(keyPlay2Model.getBaseId().toString());

                if (keyPlay2Model.getType()==2){
                    keyPlayCountModel.setQqCount(keyPlay2Model.getCount());
                }else if (keyPlay2Model.getType()==3){
                    keyPlayCountModel.setYoukuCount(keyPlay2Model.getCount());
                }else if (keyPlay2Model.getType()==4){
                    keyPlayCountModel.setIqiyiCount(keyPlay2Model.getCount());
                }
            }



            for (KeyPlayCountModel value : nowMap.values()) {
                listNow.add(value);
            }
            for (KeyPlayCountModel value : oldMap.values()) {
                listOld.add(value);
            }

            Collections.sort(listNow);
            Collections.sort(listOld);
            if (listNow.size()>0&&listOld.size()>0){
                for (int i =0;i<7;i++){
                    for (int j =0;j<7;j++){
                        if (listNow.get(i).getBaseId()==listOld.get(j).getBaseId()){
                            if (i==j){
                                listNow.get(i).setTab(0);
                            }else if (i>j){
                                listNow.get(i).setTab(-1);
                            }else {
                                listNow.get(i).setTab(1);
                            }
                        }
                    }
                }
            }

            return listNow.subList(0,7);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return listNow.subList(0,7);
    }



    private int strToNum(String str){
        int num;
        if (str.indexOf("万")>0){
            str=str.substring(0,str.indexOf("万"));
            float temp = Float.parseFloat(str);
            num = Math.round(temp*10)*1000;
        }else if (str.indexOf("亿")>0){
            str=str.substring(0,str.indexOf("亿"));
            float temp = Float.parseFloat(str);
            num = Math.round(temp*10)*10000000;
        }else {
            num=Integer.valueOf(str);
        }
        return num;
    }

    private static String getNowDate(){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr =formatter.format(date);
        dateStr+=" 00:00:00";
        return  dateStr;
    }

    public List<KeyJavaZhishuCountModel> getZhishu30() {

        SimpleDateFormat dft = new SimpleDateFormat("MM.dd");
        Date beginDate = new Date();
        //前一天
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 31);
        String dateStr=dft.format(date.getTime());
        String tempDate = dft.format(beginDate);
        List<String> dateList = new ArrayList<String>();
        dateList.add(tempDate);
        try {
            while (!tempDate.equals(dateStr)){
                date.setTime(dft.parse(tempDate));
                date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
                tempDate = dft.format(date.getTime());
                dateList.add(tempDate);
            }
            dateList = dateList.subList(1,dateList.size()-1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<KeyJavaZhishuModel> list = keyBaseJavaDaoMapper.select30(dateStr);
        Collections.reverse(list);
        Collections.reverse(dateList);
        List<KeyJavaZhishuCountModel> listCount = new ArrayList<KeyJavaZhishuCountModel>();
        Map<Integer,KeyJavaZhishuCountModel> keyZhishuMap = new HashMap<Integer,KeyJavaZhishuCountModel>();
        for (KeyJavaZhishuModel keyJavaZhishuModel:list){
            if (keyZhishuMap.get(keyJavaZhishuModel.getBaseId())==null){
                KeyJavaZhishuCountModel keyJavaZhishuCountModel = new KeyJavaZhishuCountModel();
                keyJavaZhishuCountModel.setBaseId(keyJavaZhishuModel.getBaseId());
                keyJavaZhishuCountModel.setName(keyJavaZhishuModel.getName());
                keyZhishuMap.put(keyJavaZhishuModel.getBaseId(),keyJavaZhishuCountModel);
            }
            List<Integer> baidu = keyZhishuMap.get(keyJavaZhishuModel.getBaseId()).getBaiduList();
            List<Integer> look = keyZhishuMap.get(keyJavaZhishuModel.getBaseId()).getLookList();
            List<Integer> weixin = keyZhishuMap.get(keyJavaZhishuModel.getBaseId()).getWeixinList();
            baidu.add(keyJavaZhishuModel.getBaiduNum());
            look.add(keyJavaZhishuModel.getLookNum());
            weixin.add(keyJavaZhishuModel.getWeixinNum());
        }
        for (Integer key : keyZhishuMap.keySet()) {
            keyZhishuMap.get(key).setDateList(dateList);
            List baiduList = keyZhishuMap.get(key).getBaiduList();
            List lookList = keyZhishuMap.get(key).getLookList();
            List weixinList = keyZhishuMap.get(key).getWeixinList();
            if (baiduList.size()>30){
                keyZhishuMap.get(key).setBaiduList(baiduList.subList(0,30));
            }
            if (lookList.size()>30){
                keyZhishuMap.get(key).setLookList(lookList.subList(0,30));
            }
            if (weixinList.size()>30){
                keyZhishuMap.get(key).setWeixinList(weixinList.subList(0,30));
            }
            listCount.add(keyZhishuMap.get(key));
        }

        return listCount;

    }


    public Integer upHuati(String listStr){
        return keyBaseDaoMapper.upHuati(listStr);
    }
}
